package com.dpiinc.TechPage.pen.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.dpiinc.TechPage.BaseActivity
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.constants.RxbusEvents
import com.beiji.lib.pen.PenManager
import com.beiji.lib.pen.constants.PenBusTag
import com.beiji.lib.pen.constants.PenConnectState
import com.beiji.lib.pen.model.BatteryLevelWrapper
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.hwangjr.rxbus.thread.EventThread
import kotlinx.android.synthetic.main.activity_pen_status.*
import kotlinx.android.synthetic.main.connect_failed_layout.*

/**
 * 智能笔状态页
 * Created by X on 2018/7/4.
 */
class PenStatusActivity : BaseActivity() {

    private var mFrom = ConnectPenActivity.FROM_NOTE_LIST
    private lateinit var penManager: PenManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pen_status)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        penManager = PenManager.instance
        mFrom = intent.getIntExtra(ConnectPenActivity.PARAM_FROM, ConnectPenActivity.FROM_NOTE_LIST)
        RxBus.get().register(this)
        toolbar.setNavigationOnClickListener {
            finish()
        }
        initListener()
    }

    private fun initListener() {
        penManager.setPenConnectStateListener(object : PenManager.PenConnectStateListener {
            override fun connectStateCallback(state: PenConnectState) {
                Log.i("PenStatusActivity", "connectStateCallback:$state")
                runOnUiThread {
                    if (state == PenConnectState.CONNECT_FAILED || state == PenConnectState.DISCONNECTED) {
                        ll_pen_connect_failed.visibility = View.VISIBLE
                        ll_pen_notconnect.visibility = View.GONE
                        ll_pen_isconnect.visibility = View.GONE
                        btn_reconnect.setOnClickListener {
                            ll_pen_connect_failed.visibility = View.GONE
                            bindView()
                        }
                    }
                }
            }
        })
    }

    override fun onResume() {
        super.onResume()
        bindView()
    }

    @SuppressLint("SetTextI18n")
    private fun bindView() {
        val connectPen = penManager.getConnectedPen()
        if (connectPen == null) {
            ll_pen_notconnect.visibility = View.VISIBLE
            ll_pen_isconnect.visibility = View.GONE
//            rl_connect_pen.setOnClickListener {
                val connectPenIntent = Intent(this, ConnectPenActivity::class.java)
                connectPenIntent.putExtra(ConnectPenActivity.PARAM_FROM, mFrom)
                startActivity(connectPenIntent)
                finish()
//            }
            return
        }
        ll_pen_notconnect.visibility = View.GONE
        ll_pen_isconnect.visibility = View.VISIBLE
        tv_pen_status.text = getString(R.string.label_pen_status_connected)
        pen_status_layout.visibility = View.VISIBLE
        btn_change_pen.visibility = View.VISIBLE
        tv_name.text = connectPen.name
        tv_mac_address.text = connectPen.macAddress
        setBatteryLevel(connectPen.batteryLevel)
        btn_change_pen.setOnClickListener {
            penManager.disConnect()
            val connectPenIntent = Intent(this, ConnectPenActivity::class.java)
            connectPenIntent.putExtra(ConnectPenActivity.PARAM_FROM, mFrom)
            startActivity(connectPenIntent)
            finish()
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = [(Tag(PenBusTag.BATTERY_ACHIEVED))]
    )
    @Suppress("unused")
    fun onBatteryAchieved(batteryLevelWrapper: BatteryLevelWrapper) {
        Log.i("TAG", "onBatteryAchieved: ${batteryLevelWrapper.batterLevel}")
        setBatteryLevel(batteryLevelWrapper.batterLevel)
    }

    @Subscribe
    @Suppress("unused")
    fun closePage(event: String) {
        if (event == RxbusEvents.EVENT_CLOSE_PEN_STATUS_PAGE) {
            finish()
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setBatteryLevel(percent: Int) {
        Log.i("TAG", "setBatteryLevel: $percent")
        if (percent == 0) {
            progress_bar.visibility = View.VISIBLE
            tv_battery_level.visibility = View.INVISIBLE
        } else {
            progress_bar.visibility = View.GONE
            tv_battery_level.visibility = View.VISIBLE
            tv_battery_level.text = "$percent%"
        }
//        progress_bar.visibility = View.GONE
//        tv_battery_level.visibility = View.VISIBLE
//        tv_battery_level.text = "$percent%"
    }


    override fun onDestroy() {
        RxBus.get().unregister(this)
        super.onDestroy()
    }
}
